import com.google.gson.*;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by sqy on 2018/12/11.
 */
public class JsonUtils {

    private static  String storeDir;
    private static Gson gson = new GsonBuilder().create();

    public static void main(String[] args) {
        String usedData = "bigdata.in";

        Pattern p = Pattern.compile("[.]");
        Matcher m = p.matcher(usedData);
        boolean flag = m.find();

        String[] strings = usedData.split("\\.");

        System.out.println(flag);
        if (!flag){
            System.out.println(strings[0]);
        }else {

            System.out.println(strings[0]+","+strings[1]);
        }


    }

    public static boolean checkPunctuation(String usedData){

        String tmp = usedData;
        Pattern p = Pattern.compile("[.]");
        Matcher m = p.matcher(tmp);
        boolean flag = m.find();

        return flag;
    }

    public static void saveToJson(List<AuditRule> ruleList){
        if(ruleList != null){
            Writer writer = null;
            try {
                File jsonFile = storeDir ==null? null :new File("./auditRule.json");
                writer = new FileWriter(jsonFile,false);
                gson.toJson(ruleList,writer);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(writer !=null){
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static List<AuditRule> loadFromJson(){
        ClassLoader classLoader = JsonUtils.class.getClassLoader();
        URL resource = classLoader.getResource("auditRule.json");
        String path = resource.getPath();

        List<AuditRule> ruleList = new ArrayList<>();
        File jsonFile = new File(path);
        Reader reader = null;
        try {
            reader = new FileReader(jsonFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        JsonParser jsonParser=new JsonParser();
        JsonElement jsonElement=jsonParser.parse(reader);  //将json字符串转换成JsonElement
        JsonArray jsonArray=jsonElement.getAsJsonArray();  //将JsonElement转换成JsonArray
        Iterator it=jsonArray.iterator();  //Iterator处理
        while(it.hasNext()){//循环

            jsonElement=(JsonElement) it.next(); //提取JsonElement
            AuditRule rule = gson.fromJson(jsonElement.toString(), AuditRule.class); //String转化成JavaBean
            ruleList.add(rule);  //加入List
        }

        /*if(jsonFile !=null && jsonFile.isFile() && jsonFile.canRead()){
            Reader reader =null;
            try {
                reader = new FileReader(jsonFile);
                AuditRule rule= gson.fromJson(reader,AuditRule.class);
                ruleList.add(rule);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }finally {
                if(reader !=null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }*/
        return ruleList;
    }
    public static void createRule(){
        List<AuditRule> ruleList = new ArrayList<AuditRule>();

        AuditRule rule1 = new AuditRule();
        rule1.setId(1);
        rule1.setPurpose("SchoolAnalyse");

        Set<String> usedData1 = new HashSet<String>();
        usedData1.add("userInfo.status");
        rule1.setUsedData(usedData1);

        Set<String> task1 = new HashSet<String>();
        task1.add("count");
        rule1.setTask(task1);
        rule1.setDescription("create the rule for schoolAnalyse");
        Date time = new Date();
        rule1.setCreateTime(time);
        rule1.setCreateBy("root");

        ruleList.add(rule1);

        AuditRule rule2 = new AuditRule();

        rule2.setId(2);
        rule2.setPurpose("internetAnalyse");

        Set<String> usedData2 = new HashSet<>();
        usedData2.add("internetInfo.logtime");
        usedData2.add("internetInfo.outLogtime");
        rule2.setUsedData(usedData2);

        Set<String> task2 = new HashSet<>();
        task2.add("avg");
        rule2.setTask(task2);
        rule2.setDescription("create the rule for internetAnalyse");

        Date time2 = new Date();
        rule2.setCreateTime(time2);
        rule2.setCreateBy("root");

        ruleList.add(rule2);

        AuditRule rule3 = new AuditRule();

        rule3.setId(3);
        rule3.setPurpose("internetAndbooklendAnalyse");

        Set<String> usedData3 = new HashSet<>();
        usedData3.add("booklendInfo");
        usedData3.add("internetInfo");
        rule3.setUsedData(usedData3);

        Set<String> task3 = new HashSet<>();
        task3.add("sum");
        task3.add("count");
        task3.add("rank");
        task3.add("dense_rank");

        rule3.setTask(task3);
        rule3.setDescription("create the rule for internetAndbooklendAnalyse");

        Date time3 = new Date();
        rule3.setCreateTime(time3);
        rule3.setCreateBy("root");

        ruleList.add(rule3);

        AuditRule rule4 = new AuditRule();

        rule4.setId(4);
        rule4.setPurpose("financeAnalyse");

        Set<String> usedData4 = new HashSet<>();
        usedData4.add("booklendInfo");
        usedData4.add("internetInfo");
        rule4.setUsedData(usedData4);

        Set<String> task4 = new HashSet<>();
        task4.add("max");
        task4.add("min");
        task4.add("rank");
        task4.add("dense_rank");

        rule4.setTask(task4);

        rule4.setDescription("create the rule for financeAnalyse");
        Date time4 = new Date();
        rule4.setCreateTime(time4);
        rule4.setCreateBy("root");

        ruleList.add(rule4);

        JsonUtils.saveToJson(ruleList);
    }
}
